<?php
/*
* @Author: hzwlxy
* @Email: 120235331@qq.com
* @Github: http：//www.github.com/siaoynli
* @Date: 2019/7/4 14:31
* @Version:  管理员表观察者模型
* @Description:  
*/

/*-----------observable event names-----------------*/
/* 'retrieved', 'creating', 'created', 'updating', 'updated',
/* 'saving', 'saved', 'restoring', 'restored', 'replicating',
/* 'deleting', 'deleted', 'forceDeleted',
/*----------------------------*/

namespace App\Observers;

use App\Mail\RegisterMail;
use App\Models\Admin;
use App\Models\Token;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Mail;

class AdminObserver
{

    //创建管理员的时候，自动添加一条token记录
    public function created(Admin $admin){
        $data["name"]=$admin->email;
        $data["type"]="email";
        $data["token"]=Str::random(20);
        $data["table_name"]="admins";

        //addDays会改变自身，可以使用copy方法
        $data["expire_at"]=Carbon::now()->addDays(1);
        $token=Token::create($data);
        //发送邮件
        Mail::to($admin)->send(new RegisterMail($token));
    }



}